<template>
  <div class="login-container">
    <div class="login-form">
      <h2>欢迎登录</h2>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">用户名:</label>
          <input
            type="text"
            id="username"
            v-model="username"
            placeholder="请输入用户名"
            :class="{ 'is-invalid': errorMessage &&!username }"
          />
          <div v-if="errorMessage &&!username" class="invalid-feedback">
            用户名不能为空
          </div>
        </div>
        <div class="form-group">
          <label for="password">密码:</label>
          <input
            type="password"
            id="password"
            v-model="password"
            placeholder="请输入密码"
            :class="{ 'is-invalid': errorMessage &&!password }"
          />
          <div v-if="errorMessage &&!password" class="invalid-feedback">
            密码不能为空
          </div>
        </div>
        <button type="submit">登录</button>
        <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

// 定义响应式数据
const username = ref('');
const password = ref('');
const errorMessage = ref('');

// 获取路由实例
const router = useRouter();

// 定义登录处理方法
const handleLogin = async () => {
  if (!username.value ||!password.value) {
    errorMessage.value = '用户名和密码不能为空';
    return;
  }

  try {
    // 调用 json-server 提供的接口，根据用户名和密码查询用户
    const response = await axios.get(`http://localhost:3000/users?username=${username.value}&password=${password.value}`);
    const users = response.data;

    if (users.length > 0) {
      // 账号和密码正确，跳转到 two 页面
      router.push('/index');
      errorMessage.value = '';
    } else {
      // 账号或密码错误
      errorMessage.value = '用户名或密码错误';
    }
  } catch (error) {
    console.error('请求出错:', error);
    errorMessage.value = '请求出错，请稍后重试';
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

.login-form {
  width: 400px;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  animation: fadeIn 0.5s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-form h2 {
  margin-bottom: 30px;
  font-size: 24px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-size: 16px;
  color: #666;
}

.form-group input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 5px;
  font-size: 16px;
  transition: border-color 0.3s ease;
}

.form-group input:focus {
  outline: none;
  border-color: #007bff;
  box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
}

.form-group input.is-invalid {
  border-color: #dc3545;
}

.invalid-feedback {
  color: #dc3545;
  font-size: 14px;
  margin-top: 5px;
}

button {
  background-color: #007bff;
  color: white;
  padding: 12px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s ease;
  width: 100%;
}

button:hover {
  background-color: #0056b3;
}

.error-message {
  color: #dc3545;
  font-size: 14px;
  margin-top: 15px;
  text-align: center;
}
</style>